package com.brillilab.starter.controller.aio.message;

import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.message.Message;
import com.brillilab.domain.vo.message.MessageDetailVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.message.IMessageService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/message")
public class MessageController {

	@Resource
	private IMessageService messageService;
	@Resource
	private UsersLogic usersLogic;
	
	/**
	 * 消息列表
	 * @param isRead 状态：null-全部，0-未读
	 * @return
	 */
	@GetMapping("/list")
	public ResponseVo messageList(Integer isRead, int pageNum, int pageSize,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		PageVo<Message> pageVo = messageService.getMessageList(userInfo.getId(), isRead, pageNum, pageSize);
		return ResponseVo.success(pageVo);
	}

	/**
	 * 消息详情
	 * @param messageId
	 * @param token
	 * @return
	 */
	@GetMapping("/detail")
	public ResponseVo messageDetail(Long messageId,@RequestHeader(name = StarterConstant.TOKEN) String token){

		UserInfoVo userInfo = usersLogic.getUserByToken(token);

		MessageDetailVo detail = messageService.getMessageDetail(messageId,userInfo.getLabId(),userInfo.getId());

		return ResponseVo.success(detail);
	}

	/**
	 * 设置消息已读
	 * @return
	 */
	@GetMapping("/read")
	public ResponseVo messageRead(@RequestParam Long messageId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Message message=messageService.getMessage(messageId,userInfo.getId());
		message.setIsRead(BoolEnum.TRUE.getValue());
		messageService.update(message);
		return ResponseVo.success();
	}

	/**
	 * 设置消息全部已读
	 * @return
	 */
	@GetMapping("/allRead")
	public ResponseVo messageAllRead(@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		messageService.allRead(userInfo.getId());
		return ResponseVo.success();
	}
	@GetMapping("/num")
	public ResponseVo messageCount(@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		int count = messageService.getUnReadMsgCount(userInfo.getId());
		Map<String,Integer> map  = new HashMap<>();
		map.put("num",count);
		return ResponseVo.success(map);
	}
}
